package com.tuanzi.loan.web.bean.businesssystem;

import com.google.common.collect.Lists;
import com.tuanzi.loan.business.entity.businesssystem.BusinessMain;
import com.tuanzi.loan.business.service.businesssystem.BusinessMainService;
import com.tuanzi.loan.core.aspect.Debug;
import com.tuanzi.loan.vo.request.businesssystem.BusinessMainRequest;
import com.tuanzi.loan.vo.response.businesssystem.BusinessMainResponse;
import com.tuanzi.loan.web.configuration.JSFListingBean;
import com.tuanzi.loan.web.lazy.businesssystem.BusinessMainLazyDataModel;
import lombok.Data;
import lombok.EqualsAndHashCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import javax.faces.bean.ViewScoped;
import java.util.List;

import static com.tuanzi.loan.business.constants.DictionaryCode.BUSINESS_MAIN_TYPE;

@Data
@Debug
@Component
@ViewScoped
@EqualsAndHashCode(callSuper = true)
public class BusinessMainListBean extends JSFListingBean<BusinessMainRequest, BusinessMainResponse> {

    @Autowired
    private BusinessMainService settlementCompanyService;

    @PostConstruct
    public void init() throws Exception {
        initLazyDataModel();
    }

    private void initLazyDataModel() throws Exception {
        $ = new BusinessMainRequest();
        lazyDataModel = new BusinessMainLazyDataModel($);
    }

    public void onReset() throws Exception {
        initLazyDataModel();
    }

    public String onAdd() {
        return redirect("/views/businesssystem/main-info.xhtml");
    }

    public String onSee(Long id) {
        return redirect("/views/businesssystem/main-info.xhtml?id=" + id);
    }
    public void onDelete(Long id) throws Exception {
        BusinessMain one = settlementCompanyService.findOne(id);
        settlementCompanyService.delete(one);
        addSuccessfullyMessage();
    }

    @Override
    protected List<String> getDictionaryCodes() {
        return Lists.newArrayList(BUSINESS_MAIN_TYPE);
    }
}
